<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content">
    <div class="row" style="margin-bottom: 24px">
        <span style="font-size: 24px" id="h3-drawNo"></span>&emsp;&emsp;
        <span style="font-size: 24px" id="h3-drawName"></span>
        <a id="btn-save" shiro:hasPermission="asseOrd:est:edit" href="javascript:void(0);"
           class="btn btn-info pull-right" style="margin-right: 10px">
            <span class="Bold">确定</span>
        </a>
    </div>
    <form id="sheetForm" class="form-horizontal">
        <div class="row">
            <div class="col-xs-11" style="margin-left: 4%;">
                <table class="t1 t3">
                    <thead>
                    <tr>
                        <td colspan="9">
                            <img th:src="@{/img/logo3.jpg}" width="64px" height="40px" style="margin-top: -30px">
                            <div style="display: inline-block;margin-top: 5px;margin-left: 20px">
                                <span style="letter-spacing: 24px;display: block;line-height: 20px">&nbsp;辽宁忠旺机械设备制造有限公司</span>
                                <span style="display: block;font-size: 12px;line-height: 20px">Liaoning Zhongwang Machinery Equipment Manufacturing Co., Ltd</span>
                            </div>
                        </td>
                        <td colspan="3">装配派工单</td>
                        <td style="width: 3.3%"></td>
                    </tr>
                    <tr>
                        <td style="width: 6%">生产令号</td>
                        <td style="width: 11%">
                            <select id="prodNo" class="form-control" name="prodNo"></select>
                        </td>
                        <td style="width: 7.3%">项目名称</td>
                        <td colspan="2">
                            <input id="proCode" name="proCode" type="hidden">
                            <input id="proUnit" name="proUnit" type="hidden">
                            <input id="proName" type="text" class="form-control" name="proName" readonly>
                        </td>
                        <td style="width: 6%">订单数量</td>
                        <td style="width: 7%">
                            <input id="proNum" type="text" class="form-control" name="proNum" readonly>
                        </td>
                        <td style="width: 9.1%">类型</td>
                        <td style="width: 6.9%">
                            自动化
                            <input id="special" type="hidden" name="special" value="自动化">
                        </td>
                        <td style="width: 7.9%">部分</td>
                        <td style="width: 9.8%" colspan="2">
                            <select id="partId" class="form-control" name="partId">
                                <option value="">请选择</option>
                            </select>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td rowspan="2">装配图号</td>
                        <td colspan="2" rowspan="2">
                            <input id="drawNo" type="text" class="form-control" name="drawNo" autocomplete="off">
                        </td>
                        <td rowspan="2" style="width: 6%">装配名称</td>
                        <td colspan="3" rowspan="2">
                            <input id="drawName" type="text" class="form-control" name="drawName" autocomplete="off">
                        </td>
                        <td rowspan="2">装配数量</td>
                        <td rowspan="2">
                            <input id="drawNum" type="number" step="0.1" class="form-control" name="drawNum">
                        </td>
                        <td>要求完成时间</td>
                        <td colspan="2">
                            <input id="compTime" type="text" class="form-control" name="compTime" autocomplete="off">
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>部门</td>
                        <td colspan="2">
                            <select id="deptId" class="form-control" name="deptId">
                                <option value="">请选择</option>
                                <option th:each="s : ${depts}" th:value="${s.id}"
                                        th:text="${s.name}"></option>
                            </select>
                        </td>
                        <td></td>
                    </tr>
                    </thead>
                    <tbody id="card">
                    <tr>
                        <td>注<br>意<br>事<br>项</td>
                        <td colspan="11">
                            <textarea class="form-control" name="remark" rows="20"></textarea>
                        </td>
                        <td></td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="col-xs-11" style="font-size: 14px;line-height: 200%;margin-left: 4%;">
                <div class="col-xs-3" style="padding-left: 0">提报人：[[${session.user.name}]]</div>
                <div class="col-xs-3 bottom form-inline">联系电话：
                    <input id="createTel" class="form-control" style="width:60%;border: none;border-bottom: 1px solid #000;"
                           type="text" name="createTel">
                </div>
            </div>
        </div>
    </form>

</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    /** 获取部分 */
    function getPart() {
        let code = $("#proCode").val();
        let type = $("#special").val();
        switch (type) {
            case '机械' :
            case '流体' :
            case '自动化' :
                fetchPart(code, type);
                break;
            case '外观' :
                fetchPart(code, null);
                break;
        }
    }

    /** 获取部分 */
    function fetchPart(code, type) {
        js.get({
            url: ctx + 'assemble/dispatch/partList',
            data: {
                code : code,
                type : type
            },
            success: function (result) {
                $("#partId").empty().append(`<option value="">请选择</option>`);
                result.forEach(value => {
                    if (value) {
                        $("#partId").append(`<option value="${value.id}">${value.special}-${value.proPart}</option>`);
                    }
                })
            }
        })
    }

    $(function () {

        /** 生产令号下拉选 */
        js.select2({
            id: 'prodNo',
            placeholder: '',
            ajax: {
                url: function () {
                    return ctx + 'assemble/dispatch/prodOrdList'
                },
                processResults: function (data, page) {
                    let array = data;
                    for (let i = 0; i < data.length; i++) {
                        array[i]["id"] = array[i]['prodNo'];
                        array[i]["text"] = array[i]['prodNo'];
                    }
                    return {results: array};
                }
            }
        });
        /** 生产令下拉框选中 */
        $("#prodNo").on("select2:select", function () {
            $("#proCode").val($(this).select2("data")[0].prodProCod);
            $("#proName").val($(this).select2("data")[0].prodProName);
            $("#proNum").val($(this).select2("data")[0].prodNum);
            $("#proUnit").val($(this).select2("data")[0].unit);
            getPart();
        });

        /** 保存装配派工单 */
        $("#btn-save").click(function () {
            let $this = $(this);

            js.post({
                url: ctx + "verifyProStatus/isDesignByProdNO",
                data: {prodNo: $("#prodNo").select2('data')[0].prodProCod},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        debugger
                        let res = js.vd({
                            id: 'sheetForm',
                            notEmpty: ['prodNo', 'proCode', 'partId', 'drawName', 'drawNum', 'deptId', 'createTel', 'special']
                        })
                        if (!res) {
                            return;
                        }

                        $this.attr('disabled', true);
                        js.submit({
                            formId: "sheetForm",
                            url: ctx + 'assemble/dispatch/save',
                            data: new FormData($("#sheetForm")[0]),
                            success: function (result) {
                                if (result.type === web_status.SUCCESS) {
                                    location.href = ctx + "assemble/dispatch/subPage";
                                } else {
                                    js.modal.warning(result.msg);
                                }
                            }
                        });
                        $this.removeAttr('disabled');
                    } else {
                        debugger
                        let res = js.vd({
                            id: 'sheetForm',
                            notEmpty: ['prodNo', 'proCode', 'drawName', 'drawNum', 'deptId', 'createTel', 'special']
                        })
                        if (!res) {
                            return;
                        }

                        $this.attr('disabled', true);
                        js.submit({
                            formId: "sheetForm",
                            url: ctx + 'assemble/dispatch/save',
                            data: new FormData($("#sheetForm")[0]),
                            success: function (result) {
                                if (result.type === web_status.SUCCESS) {
                                    location.href = ctx + "assemble/dispatch/subPage";
                                } else {
                                    js.modal.warning(result.msg);
                                }
                            }
                        });
                        $this.removeAttr('disabled');
                    }
                }
            })
        });

    })

</script>
</body>
</html>
